﻿<?php

//header('Content-Type: text/html; charset=UTF-8'); 

/*
$nomVille = $_REQUEST['nomVille'];
$getContinent = $_REQUEST['getContinent'];
$idContinent = $_REQUEST['idContinent'];
$idPays = $_REQUEST['idPays'];
*/

include("src/DataManager.php");

$dbManager = new DataManager();

if(isset($_REQUEST['lang'])) {
	$dbManager->setLang($_REQUEST['lang']);
}


if(isset($_REQUEST['action']) && !empty( $_REQUEST['action'])) {
	
//	header('Content-Type: text/json; charset=UTF-8;');
	
	$action = $_REQUEST['action'];
	
	//echo "{\"res\":\"sqv\"}";
	
	if($action == "save"){
		$nom = $_REQUEST['nom'];
		$adresse = $_REQUEST['adresse'];
		$codePostal = $_REQUEST['codePostal'];
		$ville = $_REQUEST['ville'];

		$res=$dbManager->insertResto($nom, $adresse, $codePostal, $ville);
		echo "{\"res\":\"$res\"}";
	}
	else if($action == "saveResto"){
		$nomResto = $_REQUEST['nom'];
		$adresse = $_REQUEST['adresse'];
		$lat = $_REQUEST['latAddress'];
		$lng = $_REQUEST['lngAddress'];		

		$res=$dbManager->insertAddressResto($nomResto, $adresse,$lat,$lng);
		echo "{\"res\":\"$res\"}";
		
	}
	else if($action == "getResto"){		
		$latRight = $_REQUEST['latRight'];
		$latLeft = $_REQUEST['latLeft'];		
		$lngRight = $_REQUEST['lngRight'];
		$lngLeft = $_REQUEST['lngLeft'];
				

		$res=$dbManager->getResto($latRight,$latLeft,$lngRight,$lngLeft);
		$content = json_encode($res);
		echo $content;
		
	}
	
}

if(isset($_REQUEST['searchText']) && !empty( $_REQUEST['searchText'])) {

	$nomVille = $_REQUEST['searchText'];
	
	$jsonArray = $dbManager->getVilleByPattern($nomVille);

	/*
	$count = count($nomsVille['id']);

	$jsonArray = array("totalCount" => "$count","nomVille" => $nomsVille);
	*/
	if(!empty($jsonArray )){
		
		$jsonArray=encodeUTF8($jsonArray);

		$content = json_encode($jsonArray);

		echo $content;
		//header("X-JSON: " . $content);

		
		}
} 

else if(isset($_REQUEST['getContinent'])){
	
	
	
	$jsonArray = $dbManager->getContinent();
	if(!empty($jsonArray )){
		// utf8_encode 
		$jsonArray=encodeUTF8($jsonArray);
				
		$content = json_encode($jsonArray);
		echo $content.";";
		
	//	echo htmlentities($content,ENT_NOQUOTES,"ISO8859-1").";";
		//header("X-JSON: " . $content);

		
		}
}

else if(isset($_REQUEST['idContinent'])){
	
	$idContinent = $_REQUEST['idContinent'];
	
	$jsonArray = $dbManager->getPays($idContinent);
	if(!empty($jsonArray )){
		// utf8_encode 
		
	//	print_r($jsonArray);
		
		$jsonArray=encodeUTF8($jsonArray);
		
		$content = json_encode($jsonArray);
		echo $content.";";
		
	//	echo htmlentities($content,ENT_NOQUOTES,"ISO8859-1").";";
		//header("X-JSON: " . $content);

		
		}
}

else if(isset($_REQUEST['idPays'])){
	
	$idPays = $_REQUEST['idPays'];
	
	$jsonArray = $dbManager->getVille($idPays);
	if(!empty($jsonArray )){
		// utf8_encode 
		
	//	print_r($jsonArray);
		
		$jsonArray=encodeUTF8($jsonArray);
		
		$content = json_encode($jsonArray);
		echo $content.";";
		
	//	echo htmlentities($content,ENT_NOQUOTES,"ISO8859-1").";";
		//header("X-JSON: " . $content);

		
		}
}


function encodeUTF8($tab) {
	
	foreach($tab as &$elem){
		foreach($elem as &$content)
			$content = utf8_encode($content);
		}
	return $tab;	
}

?>